Delete rows in loops of 1000 rows

Delete rows in loops of 1000 rows


PRINT 'Start : '+CONVERT(varchar, GETDATE(), 120)
DECLARE @rowcount INT = 1
WHILE @rowcount > 0 BEGIN
   BEGIN TRAN
      DELETE TOP (1000)
          FROM Tbl

      SET @rowcount = @@ROWCOUNT
   COMMIT TRAN
   DECLARE @msg VARCHAR(50)=CONVERT(varchar, GETDATE(), 120)
   RAISERROR (@msg, 0, 1) WITH NOWAIT
   --WAITFOR DELAY '00:00:05'
END
PRINT 'Finish : '+CONVERT(varchar, GETDATE(), 120)

Seyed Hamed Vahedi Seyed Hamed Vahedi     Tue, 26 December, 2017